unit effectselector;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
  Spin, ExtCtrls, ButtonPanel, database, LCLTranslator;

type

  { TEffectSelectorForm }

  TEffectSelectorForm = class(TForm)
    AddStateComboBox: TComboBox;
    AddBuffComboBox: TComboBox;
    AddDebuffComboBox: TComboBox;
    AddDebuffRadioButton: TRadioButton;
    AddDebuffTurnsLabel: TLabel;
    CommonEventComboBox: TComboBox;
    LearnSkillComboBox: TComboBox;
    IncreaseParamComboBox: TComboBox;
    IncreaseParameterRadioButton: TRadioButton;
    LearnSkillRadioButton: TRadioButton;
    CommonEventRadioButton: TRadioButton;
    DummyBuffsDebuffsRadioButton: TRadioButton;
    DummyOtherRadioButton: TRadioButton;
    SpecialEscapeRadioButton: TRadioButton;
    RemoveDebuffComboBox: TComboBox;
    RemoveDebuffRadioButton: TRadioButton;
    RemoveBuffRadioButton: TRadioButton;
    RemoveBuffComboBox: TComboBox;
    ResultButtonPanel: TButtonPanel;
    DummyHpMpTpRadioButton: TRadioButton;
    DummyStateRadioButton: TRadioButton;
    AddBuffRadioButton: TRadioButton;
    AddBuffTurnsLabel: TLabel;
    RemoveStatePercentLabel: TLabel;
    RemoveStateRadioButton: TRadioButton;
    RemoveStateComboBox: TComboBox;
    EffectPageControl: TPageControl;
    GainTpRadioButton: TRadioButton;
    AddStateRadioButton: TRadioButton;
    AddStatePercentLabel: TLabel;
    RecoverHpPlusLabel: TLabel;
    RecoverMpPlusLabel: TLabel;
    RecoverMpConstSpinEdit: TSpinEdit;
    RecoverMpRadioButton: TRadioButton;
    RecoverMpPercentageSpinEdit: TSpinEdit;
    RecoverHpPercentLabel: TLabel;
    RecoverMpPercentLabel: TLabel;
    RecoverHpRadioButton: TRadioButton;
    HpMpTpTabSheet: TTabSheet;
    RecoverHpPercentageSpinEdit: TSpinEdit;
    RecoverHpConstSpinEdit: TSpinEdit;
    GainTpSpinEdit: TSpinEdit;
    AddStateRateSpinEdit: TSpinEdit;
    RemoveStateRateSpinEdit: TSpinEdit;
    AddBuffTurnsSpinEdit: TSpinEdit;
    AddDebuffTurnsSpinEdit: TSpinEdit;
    IncreaseParamSpinEdit: TSpinEdit;
    StateTabSheet: TTabSheet;
    ParamTabSheet: TTabSheet;
    OtherTabSheet: TTabSheet;
    procedure AddBuffRadioButtonChange(Sender: TObject);
    procedure AddDebuffRadioButtonChange(Sender: TObject);
    procedure AddStateRadioButtonChange(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure CommonEventRadioButtonChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure GainTpRadioButtonChange(Sender: TObject);
    procedure IncreaseParameterRadioButtonChange(Sender: TObject);
    procedure LearnSkillRadioButtonChange(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure RecoverHpRadioButtonChange(Sender: TObject);
    procedure RecoverMpRadioButtonChange(Sender: TObject);
    procedure RemoveBuffRadioButtonChange(Sender: TObject);
    procedure RemoveDebuffRadioButtonChange(Sender: TObject);
    procedure RemoveStateRadioButtonChange(Sender: TObject);
    procedure SpecialEscapeRadioButtonChange(Sender: TObject);
  private
    Db: TDatabase;
    ResultWasAccepted: Boolean;
    procedure PresetValues(EffectCode: Integer = -1; DataId: Integer = 0; Value1: Double = 0.0; Value2: Double = 0.0);
    procedure RepositionDummyRatioButtons;
    procedure UnsetOptionsOnOtherPages(PageToKeep: Integer);
    function GetSelectedEffectCode: Integer;
    function GetDataId(EffectCode: Integer): Integer;
    function GetValue1(EffectCode: Integer): Double;
    function GetValue2(EffectCode: Integer): Double;
    procedure SetSelectedValues;
    procedure UnsetOptionsOnUnselectedPages;
  public
    SelectedCode: Integer;
    SelectedDataId: Integer;
    SelectedValue1: Double;
    SelectedValue2: Double;
    function ShowEffectSelection(ADb: TDatabase; EffectCode: Integer = -1; DataId: Integer = 0; Value1: Double = 0.0; Value2: Double = 0.0): Boolean;
  end;

const
  EFFECT_PAGE_HPMPTP = 0;
  EFFECT_PAGE_STATE = 1;
  EFFECT_PAGE_BUFF_DEBUFF = 2;
  EFFECT_PAGE_OTHER = 3;

var
  EffectSelectorForm: TEffectSelectorForm;

implementation

uses
  comboboxhelper;

{$R *.lfm}

{ TEffectSelectorForm }

procedure TEffectSelectorForm.FormHide(Sender: TObject);
begin
  SetSelectedValues;

  UnsetOptionsOnOtherPages(-1);
  AddStateComboBox.Items.Clear;
  RemoveStateComboBox.Items.Clear;
  AddBuffComboBox.Items.Clear;
  AddDebuffComboBox.Items.Clear;
  RemoveBuffComboBox.Items.Clear;
  RemoveDebuffComboBox.Items.Clear;
  IncreaseParamComboBox.Items.Clear;
  LearnSkillComboBox.Items.Clear;
  CommonEventComboBox.Items.Clear;
end;

procedure TEffectSelectorForm.AddStateRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillStateComboBox(AddStateComboBox, Db, True);
  AddStateComboBox.Enabled := AddStateRadioButton.Checked;
  AddStateRateSpinEdit.Enabled := AddStateRadioButton.Checked;
end;

procedure TEffectSelectorForm.CancelButtonClick(Sender: TObject);
begin
  ResultWasAccepted := False;
  Close
end;

procedure TEffectSelectorForm.CommonEventRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillCommonEventComboBox(CommonEventComboBox, Db);
  CommonEventComboBox.Enabled := CommonEventRadioButton.Checked;
end;

procedure TEffectSelectorForm.FormCreate(Sender: TObject);
begin
  RepositionDummyRatioButtons;
  UnsetOptionsOnOtherPages(-1);
end;

procedure TEffectSelectorForm.AddBuffRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(AddBuffComboBox, Db);
  AddBuffComboBox.Enabled := AddBuffRadioButton.Checked;
  AddBuffTurnsSpinEdit.Enabled := AddBuffRadioButton.Checked;
end;

procedure TEffectSelectorForm.AddDebuffRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(AddDebuffComboBox, Db);
  AddDebuffComboBox.Enabled := AddDebuffRadioButton.Checked;
  AddDebuffTurnsSpinEdit.Enabled := AddDebuffRadioButton.Checked;
end;

procedure TEffectSelectorForm.GainTpRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  GainTpSpinEdit.Enabled := GainTpRadioButton.Checked;
end;

procedure TEffectSelectorForm.IncreaseParameterRadioButtonChange(Sender: TObject
  );
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(IncreaseParamComboBox, Db);
  IncreaseParamComboBox.Enabled := IncreaseParameterRadioButton.Checked;
  IncreaseParamSpinEdit.Enabled := IncreaseParameterRadioButton.Checked;
end;

procedure TEffectSelectorForm.LearnSkillRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillSkillComboBox(LearnSkillComboBox, Db);
  LearnSkillComboBox.Enabled := LearnSkillRadioButton.Checked;
end;

procedure TEffectSelectorForm.OKButtonClick(Sender: TObject);
begin
  ResultWasAccepted := True;
  Close
end;

procedure TEffectSelectorForm.RecoverHpRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  RecoverHpConstSpinEdit.Enabled := RecoverHpRadioButton.Checked;
  RecoverHpPercentageSpinEdit.Enabled := RecoverHpRadioButton.Checked;
end;

procedure TEffectSelectorForm.RecoverMpRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  RecoverMpConstSpinEdit.Enabled := RecoverMpRadioButton.Checked;
  RecoverMpPercentageSpinEdit.Enabled := RecoverMpRadioButton.Checked;
end;

procedure TEffectSelectorForm.RemoveBuffRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(RemoveBuffComboBox, Db);
  RemoveBuffComboBox.Enabled := RemoveBuffRadioButton.Checked;
end;

procedure TEffectSelectorForm.RemoveDebuffRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillParameterComboBox(RemoveDebuffComboBox, Db);
  RemoveDebuffComboBox.Enabled := RemoveDebuffRadioButton.Checked;
end;

procedure TEffectSelectorForm.RemoveStateRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
  FillStateComboBox(RemoveStateComboBox, Db);
  RemoveStateComboBox.Enabled := RemoveStateRadioButton.Checked;
  RemoveStateRateSpinEdit.Enabled := RemoveStateRadioButton.Checked;
end;

procedure TEffectSelectorForm.SpecialEscapeRadioButtonChange(Sender: TObject);
begin
  UnsetOptionsOnUnselectedPages;
end;

procedure TEffectSelectorForm.PresetValues(EffectCode: Integer;
  DataId: Integer; Value1: Double; Value2: Double);
begin
  case EffectCode of
    EFFECT_RECOVER_HP: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_HPMPTP;
        RecoverHpRadioButton.Checked := True;
        RecoverHpPercentageSpinEdit.Value := Round(Value1 * 100);
        RecoverHpConstSpinEdit.Value := Round(Value2);
      end;
    EFFECT_RECOVER_MP: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_HPMPTP;
        RecoverMpRadioButton.Checked := True;
        RecoverMpPercentageSpinEdit.Value := Round(Value1 * 100);
        RecoverMpConstSpinEdit.Value := Round(Value2);
      end;
    EFFECT_GAIN_TP: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_HPMPTP;
        GainTpRadioButton.Checked := True;
        GainTpSpinEdit.Value := Round(Value1);
      end;

    EFFECT_ADD_STATE: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_STATE;
        AddStateRadioButton.Checked := True;
        AddStateComboBox.ItemIndex := DataId;
        AddStateRateSpinEdit.Value := Round(Value1 * 100);
      end;
    EFFECT_REMOVE_STATE: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_STATE;
        RemoveStateRadioButton.Checked := True;
        RemoveStateComboBox.ItemIndex := DataId - 1;
        RemoveStateRateSpinEdit.Value := Round(Value1 * 100);
      end;

    EFFECT_ADD_BUFF: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_BUFF_DEBUFF;
        AddBuffRadioButton.Checked := True;
        AddBuffComboBox.ItemIndex := DataId;
        AddBuffTurnsSpinEdit.Value := Round(Value1);
      end;
    EFFECT_ADD_DEBUFF: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_BUFF_DEBUFF;
        AddDebuffRadioButton.Checked := True;
        AddDebuffComboBox.ItemIndex := DataId;
        AddDebuffTurnsSpinEdit.Value := Round(Value1);
      end;
    EFFECT_REMOVE_BUFF: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_BUFF_DEBUFF;
        RemoveBuffRadioButton.Checked := True;
        RemoveBuffComboBox.ItemIndex := DataId;
      end;
    EFFECT_REMOVE_DEBUFF: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_BUFF_DEBUFF;
        RemoveDebuffRadioButton.Checked := True;
        RemoveDebuffComboBox.ItemIndex := DataId;
      end;

    EFFECT_SPECIAL: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_OTHER;
        SpecialEscapeRadioButton.Checked := True;
      end;
    EFFECT_GROW: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_OTHER;
        IncreaseParameterRadioButton.Checked := True;
        IncreaseParamComboBox.ItemIndex := DataId;
        IncreaseParamSpinEdit.Value := Round(Value1);
      end;
    EFFECT_LEARN_SKILL: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_OTHER;
        LearnSkillRadioButton.Checked := True;
        LearnSkillComboBox.ItemIndex := DataId - 1;
      end;
    EFFECT_COMMON_EVENT: begin
        EffectPageControl.ActivePageIndex := EFFECT_PAGE_OTHER;
        CommonEventRadioButton.Checked := True;
        CommonEventComboBox.ItemIndex := DataId - 1;
    end;
    //TODO: preset data for remaining effects
  end;
end;

procedure TEffectSelectorForm.RepositionDummyRatioButtons;
begin
  // I haven't found a better way to unselect all radio buttons, so this will do
  DummyHpMpTpRadioButton.Left := -100;
  DummyStateRadioButton.Left := -100;
  DummyBuffsDebuffsRadioButton.Left := -100;
  DummyOtherRadioButton.Left := -100;
end;

procedure TEffectSelectorForm.UnsetOptionsOnOtherPages(PageToKeep: Integer);
begin
  if PageToKeep <> EFFECT_PAGE_HPMPTP then
    DummyHpMpTpRadioButton.Checked := True;
  if PageToKeep <> EFFECT_PAGE_STATE then
    DummyStateRadioButton.Checked := True;
  if PageToKeep <> EFFECT_PAGE_BUFF_DEBUFF then
    DummyBuffsDebuffsRadioButton.Checked := True;
  if PageToKeep <> EFFECT_PAGE_OTHER then
    DummyOtherRadioButton.Checked := True;
end;

function TEffectSelectorForm.GetSelectedEffectCode: Integer;
begin
  if RecoverHpRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_RECOVER_HP
  else if RecoverMpRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_RECOVER_MP
  else if GainTpRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_GAIN_TP

  else if AddStateRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_ADD_STATE
  else if RemoveStateRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_REMOVE_STATE

  else if AddBuffRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_ADD_BUFF
  else if AddDebuffRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_ADD_DEBUFF
  else if RemoveBuffRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_REMOVE_BUFF
  else if RemoveDebuffRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_REMOVE_DEBUFF

  else if SpecialEscapeRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_SPECIAL
  else if IncreaseParameterRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_GROW
  else if LearnSkillRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_LEARN_SKILL
  else if CommonEventRadioButton.Checked then
    GetSelectedEffectCode := EFFECT_COMMON_EVENT
  else
    GetSelectedEffectCode := 0
end;

function TEffectSelectorForm.GetDataId(EffectCode: Integer): Integer;
begin
  case EffectCode of
    EFFECT_ADD_STATE: GetDataId := AddStateComboBox.ItemIndex;
    EFFECT_REMOVE_STATE: GetDataId := RemoveStateComboBox.ItemIndex + 1;

    EFFECT_ADD_BUFF: GetDataId := AddBuffComboBox.ItemIndex;
    EFFECT_ADD_DEBUFF: GetDataId := AddDebuffComboBox.ItemIndex;
    EFFECT_REMOVE_BUFF: GetDataId := RemoveBuffComboBox.ItemIndex;
    EFFECT_REMOVE_DEBUFF: GetDataId := RemoveDebuffComboBox.ItemIndex;

    EFFECT_SPECIAL: GetDataId := SPECIAL_EFFECT_ESCAPE;
    EFFECT_GROW: GetDataId := IncreaseParamComboBox.ItemIndex;
    EFFECT_LEARN_SKILL: GetDataId := LearnSkillComboBox.ItemIndex + 1;
    EFFECT_COMMON_EVENT: GetDataId := CommonEventComboBox.ItemIndex + 1;
  else
    GetDataId := 0;
  end;
end;

function TEffectSelectorForm.GetValue1(EffectCode: Integer): Double;
begin
  case EffectCode of
    EFFECT_RECOVER_HP: GetValue1 := RecoverHpPercentageSpinEdit.Value / 100.0;
    EFFECT_RECOVER_MP: GetValue1 := RecoverMpPercentageSpinEdit.Value / 100.0;
    EFFECT_GAIN_TP: GetValue1 := GainTpSpinEdit.Value;

    EFFECT_ADD_STATE: GetValue1 := AddStateRateSpinEdit.Value / 100.0;
    EFFECT_REMOVE_STATE: GetValue1 := RemoveStateRateSpinEdit.Value / 100.0;

    EFFECT_ADD_BUFF: GetValue1 := AddBuffTurnsSpinEdit.Value;
    EFFECT_ADD_DEBUFF: GetValue1 := AddDebuffTurnsSpinEdit.Value;

    EFFECT_GROW: GetValue1 := IncreaseParamSpinEdit.Value;
  else
    GetValue1 := 0;
  end;
end;

function TEffectSelectorForm.GetValue2(EffectCode: Integer): Double;
begin
  case EffectCode of
    EFFECT_RECOVER_HP: GetValue2 := RecoverHpConstSpinEdit.Value;
    EFFECT_RECOVER_MP: GetValue2 := RecoverMpConstSpinEdit.Value;
  else
    GetValue2 := 0;
  end;
end;

procedure TEffectSelectorForm.SetSelectedValues;
begin
  SelectedCode := GetSelectedEffectCode;
  SelectedDataId := GetDataId(SelectedCode);
  SelectedValue1 := GetValue1(SelectedCode);
  SelectedValue2 := GetValue2(SelectedCode);
end;

procedure TEffectSelectorForm.UnsetOptionsOnUnselectedPages;
begin
  UnsetOptionsOnOtherPages(EffectPageControl.ActivePageIndex);
end;

function TEffectSelectorForm.ShowEffectSelection(ADb: TDatabase;
  EffectCode: Integer; DataId: Integer; Value1: Double; Value2: Double
  ): Boolean;
begin
  ResultWasAccepted := False;
  Db := ADb;
  //TODO
  PresetValues(EffectCode, DataId, Value1, Value2);

  ShowModal;

  ShowEffectSelection := ResultWasAccepted;
end;

end.

